Git hands-on session within RStudio
Stijn Van Hoey, Peter Desmet, Thierry Onkelinx
Git for RStudio setup
Tell RStudio where to find the Git installation.
- Go to
Tools > Global Options
- Click on
Git/SVN.
![RStudio setup for git]()
Git for RStudio setup
Check Enable version control interface for RStudio projects. Set the path to the Git executable that you just installed. Open a shell, if you don’t know where Git is installed. Type where git and hit enter (Windows users).
The path should be something like: C:/Program Files (x86)/Git/bin/git.exe
Git for RStudio setup: git configuration
- Go to
Tools > Shell to open the Git Shell
- Tell Git your Github (!) username and GitHub email
![RStudio setup git shell]()
Git for RStudio setup: git configuration
- Go to
Tools > Shell to open the Git Shell
- Tell Git your Github (!) username and GitHub email:
In the shell, type the following two commands:
git config --global user.email "my.name@inbo.be"
git config --global user.name "mygithubusername"
Use your GitHub username!
Clone a repo to work locally
- Copy the repository HTTPS url
![Copy the https link of a repo]()
File > New Project..., select Version Control, choose Git
- Provide the repository HTTPS link, select desired directory
![Clone a repo in RStudio]()
.gitignore
- RStudio will always add
.gitignore if it doesn’t exists
- Go to
git pane, right click .Proj and select ÃŒgnore
- Update
.gitignore and click Save. More details later.
![Ignore a file]()
![Update .gitignore]()
Make a commit
- Update the
README.md
- Commit your changes
- Go to
git pane, check .gitignore and click commit
![Local commit]()
Make a commit
- Add a commit message and click commit
![Local commit]()
Make a commit
- Click
Close to remove the commit summary
![Local commit]()
Make a commit
- Add a commit message and click commit
![Local commit]()
Push the changes to GitHub
- Note that the
git pane displays your branch is ahead of ‘origin/master’ by 2 commits
- Click
push in the git pane
![Push]()
Go to your repo on GitHub to verify
![Verify your commits on GitHub]()
Store your credentials when using https
- Click on the
more button in the git pane and select shell
- Type
git config --global credential.helper store
- Type
exit to quit the shell
- The next time
git needs your credentials, it will ask them one more time and store them
![Store credentials]()
Create logical commits
- Make 2 unrelated changes to your
README.md locally
- Create one commit for each change.
- Do NOT push (yet)
Create logical commits
- Click
Commit in the git pane
- Select the changes you want to commit and click
stage selection
- Add a
commit message and click Commit
![Logical commit 1]()
Create logical commits
stage xyz: add the xyz from the commit
unstage xyz: remove the xyz from the commit
discard xyz: revert the changes in the xyz (can’t be undone!!!)
xyz can be
- a single
line
- a
selection of lines
- a
chunk
- RStudio will automatically split the changes in chunks
- chunks are defined by 10 unchanged lines between changes
Create a conflict
Update your README.md on GitHub,
on a line you also edited locally
![Commit to create conflict]()
Conflict!
Try to push (click push) your local changes
![Conflict]()
Keep calm and resolve conflict
- Click
Pull to download the changes from Github
![Conflict]()
Keep calm and resolve conflict
Open README.md in RStudio:
<<<<<<< HEAD
A simple analysis to visualize my favourite fruit colour.
=======
A simple analysis to discover my favourite fruit color.
>>>>>>> origin/master
Choose what you want to keep:
A simple analysis to discover my favourite fruit colour.
Commit and click push
Add a file
- Add a
fruits.csv file in a /data directory
- Link to this file in the
README.md
![Add a file]()
Add a file within a directory
- Staging a new directory will stage all files in the directory
- You can’t stage empty directories
![Add a file]()
Adapt last commit
- Warning: don’t do this on commits that have been pushed. That would result in conflicts.
- Make a change in
fruits.csv
Commit as usual but check amend previous commit under the commit message box
![Add a file]()
View history
- Click on
history in the git pane ![View history]()
View history
![View history]()
Create a branch to experiment
- Create a NEW branch
- Add an analysis file in a
/src directory
- Commit
- Switch between branches
Create a branch to experiment
- Open the git shell
- Create a branch by a checkout to new branch
git checkout -b analysis-script
- Get an overview of your branches:
git branch
![Create branch]()
Effect of changing switching branches
Add a file to the repository in a src-directory (see earlier)
- Go back to the status of the
master branch
- Click on
analysis-script in the git pane and select (LOCAL BRANCHES) -> master ![Switch branch]()
![Switch branch]()
Effect of changing switching branches
- Verify your local directory structure: where are your files?
- Go back to the status of the new branch (e.g.Â
analysis-script)
- Verify your local directory structure again: where are your files?
First push of a new branch
- Notice that the
pull and push buttons in the git pane are grayed out
- Open the Git Shell
- Type
git push -u origin analysis-script
This will also activate the pull and push buttons
![First push branch]()
Create a pull request to include your work
- Click
push
- At Github, browse to you repo…
- Create a
pull request
Create a pull request to include your work
![Check Github message pull request]()
Create a pull request to include your work
![Adapt Github message and start pull request]()
Review your pull request
![See pull request]()
Review your pull request
- Review
- If OK, merge pull request
- Delete branch
Include your accepted work to local master
- Switch to
master branch
- Click
pull.
![git pull]()
Exclude files
- in any programming language, some files are derivatives
- sensitive information (passwords,…)
- a folder with large data files that should not be in the history (and backuped elsewhere!)
- a temp/ folder for just garbage you create/…
.gitignore to the rescue!
Exclude files
![Ignore a file]()
Note that you can use wild cards e.g. *.Rproj
![Update .gitignore]()
Teaming up
- Team up with two or three persons
- Let one person invite the others, provide them with read/write access as explained in this tutorial
Invite collaborators
![Invite collaborator]()
Invite collaborators
![react to collaborator]()
Collaboration: issues
- Raise an issue online
- Provide issues with an appropriate label
- Assign your colleague to the issue
Collaboration: online adaptations
- Make an adaptation to an online file
- Propose a pull request and assign your collaborator
- Merge the adaptation to the
master branch
Collaboration: local adaptations
Working local - merging online
- As collaborator,
clone the other repository to your local computer
- Create a new
branch with a different name
- Adapt the content of a file
commit your adaptation
push your branch to the remote repository
- Go to GitHub and make a
pull request
- Revise the work and
merge online when appropriate
- Update your local work
- Check if all adaptations are represented in your local files
Tip: All functionalities are available in the previous sections
Release
Once you are satisfied with the status of your analysis, it makes sense to create a release:
- For publications (DOI)
- For code/software development versions
- For course notes
Follow this tutorial to create a release.
Some more advice:
- Commit often, make small commits
- Don’t mix changes in 1 commit
- Think about your commit messages
- Keep your code clean, avoid huge one-liners
- Use branches (!)
- Don’t keep long-lived branches (form of technical debt)
There’s no such thing, as a free lunch…
…but if you’re hungry: